﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Example for using Clavid OAUTH SuisseID classes.
///
/// $Id: Default.aspx.cs 13 2011-10-09 15:50:26Z telbie $
/// </summary>
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        ClavidOAuthSuisseID oAuth = new ClavidOAuthSuisseID();

        if (Request["oauth_token"] == null)
        {
            //Redirect the user to Clavid for SuisseID authentication.
            //Using oauth_callback for local testing.
            oAuth.CallBackUrl = ConfigurationManager.AppSettings["callBackUrl"];
            oAuth.setUserLanguage("de");
            oAuth.setRequiredAttribute(ClavidOAuthSuisseID.SUISSE_ID_NUMBER);
            oAuth.setRequiredAttribute(ClavidOAuthSuisseID.SUISSE_ID_EMAIL);
            oAuth.setOptionalAttribute(ClavidOAuthSuisseID.SUISSE_ID_NAME);

            string tokenSecret;
            string redirUrl = oAuth.AuthorizationLinkGet(out tokenSecret);
            Session["clavidTokenSecret"] = tokenSecret;
            Response.Redirect(redirUrl);
        }
        else
        {
            //Get the access token and the Clavid specific parameters.
            string tokenSecret = (string)Session["clavidTokenSecret"];
            oAuth.AccessTokenGet(tokenSecret, Request["oauth_token"], Request["oauth_verifier"]);
            Session["suisseid_number"] = oAuth.SuisseIdNumber;
            Session["suisseid_email"] = oAuth.SuisseIdEMail;
            Session["suisseid_name"] = oAuth.SuisseIdName;
        }
    }
}